package com.etoak.config;

import com.etoak.properties.ImageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

/**
 * @author zhaochenhao
 * @since 2024/8/12 21:30
 * @version 1.0
 */
@Configuration
public class webFluxConfig implements WebFluxConfigurer {

    @Autowired
    ImageProperties imageProperties;

    /**
     * ResourceHandlerRegistry 用于注册资源处理器,资源处理器负责将请求的 URL 映射到特定的静态资源文件夹。
     * @param registry 资源处理器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        String mapping = imageProperties.getLocation().endsWith("/") ?
                imageProperties.getPrefix() + "**" :
                imageProperties.getPrefix()+ "/**";

        String location = imageProperties.getLocation().endsWith("/") ?
                "file:" + imageProperties.getLocation() :
                "file:" + imageProperties.getLocation() + "/";
        registry.addResourceHandler(mapping)
                .addResourceLocations(location);
    }
}
